﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using KPIS.GERP.SECURITY.DAL;
using KPIS.GERP.SECURITY.MODAL;
using KPIS.GERP.SECURITY.MODEL;

namespace KPIS.GERP.SECURITY.BLL
{
    public class UserRoleBLL
    {
        private UserRoleDAL DAL = new UserRoleDAL();

        public UserRoleBLL()
        {

        }
        public DataTable LoadUserRole(Nullable<int> Sequence, Nullable<int> UserSequence)
        {
            return DAL.LoadUserRole(Sequence, UserSequence);
        }
        public DataTable LoadUserRoleUnion(Nullable<int> Sequence, Nullable<int> UserSequence, string SystemID)
        {
            return DAL.LoadUserRoleUnion(Sequence, UserSequence, SystemID);
        }
        public void Insert(int UserSequence, int RoleSequence, int CreatedBy)
        { 
            try
            {
                DAL.Insert(UserSequence, RoleSequence, CreatedBy);
            }
            catch
            {
                throw;
            }
        }
        public void Update(int Sequence, int UserSequence, int RoleSequence, int UpdatedBy)
        {
            try
            {
                DAL.Update(Sequence, UserSequence, RoleSequence, UpdatedBy);
            }
            catch
            {
                throw;
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            try
            {
                DAL.Delete(Sequence, UpdatedBy);
            }
            catch
            {
                throw;
            }
        }

         public List<UserWithRoles> GetUserWithRoles()
         {
            var u =  DAL.GetUsersAsObjectUserWithRoles();
            var r = DAL.GetUsersRoles();
            foreach(var eachU in u)
            {
                // 3 modules are hard code, every user have it.
                eachU.RolesInUser = r.Where(o => o.UserSeq == eachU.UserSeq).ToList();
                eachU.RolesInUser.Add(new RolesInUser {System = "ESS", RoleDescription = "ผู้ใช้ระบบบริการออนไลน์", UserSeq = eachU.UserSeq});
                eachU.RolesInUser.Add(new RolesInUser {System = "GIMS", RoleDescription = "แจ้งความประสงค์ขอซื้อจ้าง", UserSeq = eachU.UserSeq});
                eachU.RolesInUser.Add(new RolesInUser {System = "GBMS", RoleDescription = "งบประมาณ", UserSeq = eachU.UserSeq});
                eachU.RolesInUser.Add(new RolesInUser {System = "APPROVAL", RoleDescription = "ระบบขออนุมัติ", UserSeq = eachU.UserSeq});
            }

             return u;
         }
    }
}
